package com.ruoyi.web.controller.system;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.jta.WebSphereUowTransactionManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/system/work")
public class TestControllerAll extends BaseController {
    private String prefix = "system/worksys";

    public static void main(String[] args) {
        System.out.println();
    }
    @Value("${newUrl}")
    private String newUrl;

    @GetMapping
    public String add() {
        return prefix + "/test";
    }

    @RequestMapping("/test")
    public AjaxResult test(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();

        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        List<String> extList = Arrays.asList(".jpg", ".png", ".jpeg", ".gif");
        if (!extList.contains(suffixName)) {
            return toAjax(false);
        }

        fileName = UUID.randomUUID().toString().replace("-", "") + System.currentTimeMillis() + suffixName;

        //String url = "localhost:9004"+s + fileName;

        File dest = new File(newUrl + fileName);

        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            return toAjax(true);
        } catch (IOException e) {
            throw new RuntimeException();
        }
    }
}

